package com.demo.spring.IOC的源码分析;

import java.beans.PropertyDescriptor;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.stereotype.Component;

/**
 * 自定义实例化bean后处理器适配器，复写postProcessBeforeInstantiation、postProcessBeforeInstantiation、postProcessPropertyValues分别对应实例化Bean前调用、实例化Bean后调用、设置某个属性时调用
 * 注意：InstantiationAwareBeanPostProcessorAdapter ，这里是实例化Instantiation，不是初始化Initialization！！！
 * @ClassName:MyInstantiationAwareBeanPostProcessor
 * @Description:BeanPostProcessor的子类，增加了更多自定方法
 * @date 2017年10月25日下午7:09:15
 */
@Component
public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {

	public MyInstantiationAwareBeanPostProcessor() {
		super();
		System.out.println("这是InstantiationAwareBeanPostProcessorAdapter实现类构造器！！");
	}

	// 接口方法、实例化Bean之前调用
	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		if(beanName.equals("messageService")){
			System.out.println("InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法[实例化Bean之前调用 ]beanname=" + beanName);
		}
		return null;
	}

	// 接口方法、实例化Bean之后调用
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if(beanName.equals("messageService")){
			System.out.println("InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法[实例化Bean之后调用]beanname=" + beanName);
		}
		return bean;
	}

	// 接口方法、设置某个属性时调用
	@Override
	public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
		if(beanName.equals("messageService")){
			System.out.println("InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法 pvs=" + pvs + ",beanName=" + beanName);
		}
		return pvs;
	}
}